package com.hudongwx.shansun.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hudongwx.shansun.R;
import com.hudongwx.shansun.utlis.API;
import com.hudongwx.shansun.utlis.MyJSONobject;

import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by hudongwx on 16-10-31.
 */
public class FinishProjectPopupWindow extends PopupWindow {

    private View mView;
    private TextView tv1, tv2;
    private Button btn;

    public FinishProjectPopupWindow(final Context context) {

        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.setting_contactme, null);
        tv1 = (TextView) mView.findViewById(R.id.setting_contact_tv_1);
        tv2 = (TextView) mView.findViewById(R.id.setting_contact_tv_2);
        btn = (Button) mView.findViewById(R.id.setting_contact_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        RequestParams params = new RequestParams(API.aboutme());
        x.http().get(params, new Callback.CommonCallback<MyJSONobject>() {
            @Override
            public void onSuccess(final MyJSONobject result) {
                try {

                    tv1.setText(result.getString("customerPhone"));

                    tv1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_DIAL);
                            try {
                                intent.setData(Uri.parse("tel:" + result.getString("customerPhone")));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            context.startActivity(intent);
                        }
                    });

                    tv2.setText(result.getString("customerEmail"));
                    tv2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Uri uri = null;
                            try {
                                uri = Uri.parse("mailto:"+result.getString("customerEmail"));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            Intent it = new Intent(Intent.ACTION_SENDTO, uri);
                            context.startActivity(it);
                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
        this.setContentView(mView);
        //设置PopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置PopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.Animation);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }


}
